package cc.chengheng.config;

import cc.chengheng.filter.TokenAuthenticationFilter;
import cc.chengheng.filter.TokenAuthorizationFilter;
import cc.chengheng.token.TokenManager;
import cc.chengheng.util.UnauthorizedEntryPoint;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.ForwardAuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import javax.annotation.Resource;
import javax.sql.DataSource;

@Configuration
@EnableWebSecurity // 开启方法控制权限
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource
    private UserDetailsService userDetailsService;

    @Resource
    private TokenManager tokenManager;


    private AuthenticationManager authenticationManager;

    @Resource
    private DataSource dataSource;

    public WebSecurityConfig() {
    }

    // 数据库, 记住我用的
    @Bean
    public PersistentTokenRepository persistentTokenRepository() {
        JdbcTokenRepositoryImpl jdbcTokenRepository = new JdbcTokenRepositoryImpl();
        jdbcTokenRepository.setDataSource(dataSource);
        return jdbcTokenRepository;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.exceptionHandling()

                //没有权限访问时调用自定义的处理类
                .authenticationEntryPoint(new UnauthorizedEntryPoint())

                .and().csrf().disable()

                .authorizeRequests()
                .anyRequest().authenticated()

                //退出路径
                .and()
                .logout().logoutUrl("/admin/acl/index/logout");
//                .addLogoutHandler(new TokenLogoutHandler(tokenManager, redisTemplate)) // 调用退出时的处理器

                http

                // 授权过滤器
                .addFilterAfter(new TokenAuthorizationFilter(authenticationManager(), tokenManager), UsernamePasswordAuthenticationFilter.class)

                // 认证过滤器
                .addFilter(new TokenAuthenticationFilter(tokenManager, authenticationManager()))



                .httpBasic();
    }


    /**
     * 在http也能设置, 不用授权就可以执行
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/login");
    }
}

